<?php

/**
 * 关键字过滤
 *
 * @author suning <snsnsky@gmail.com>
 */

use \Common\Bridge;

define('SITE_AUTH', 'safety-filter');
require('../../../common.inc.php');

class filter extends \lib\core\AdminAction
{
    public function doDefault()
    {

        $page = max(1, intval($_GET['page']));

        $offset = ($page - 1) * self::PAGE_SIZE;
        $pagesize = self::PAGE_SIZE;

        $pdo = Bridge::pdo(Bridge::DB_ADMIN_R);
        $query = $pdo->prepare('select count(*) from Filter');
        $query->execute();
        $total = $query->fetchColumn();

        $query = $pdo->prepare("select * from Filter order by Id DESC limit $offset,$pagesize");
        $query->execute();
        $filter = $query->fetchAll();
        $pagination = $this->pagination($total, self::PAGE_SIZE, $page);
        $smarty = $this->app->page();
        $smarty->value('filter', $filter);
        $smarty->value('pages', $pagination);
        $smarty->output('safety/filter.tpl');
    }

    /**
     *多选删除
     */
    public function doMoredel()
    {
        $dbw = Bridge::pdo(Bridge::DB_ADMIN_W);
        $delid = explode(',', $_POST['id']);
        $arrid = '';
        foreach ($delid as $v)
        {
            $arrid .= intval($v) . ',';
            if (!$v) continue;
        }
        $arrid = rtrim($arrid, ',');
        if ($arrid)
        {
            $query = $dbw->query("delete from Filter where Id in($arrid)");
            $this->adminLog('删除关键字', 0, 'del', serialize($delid));
            if ($query->rowCount() > 0)
            {
                self::output(['title'=>'删除成功','type'=>'success']);
            }
        }
        self::output(['title'=>'删除失败','type'=>'error']);
    }

    /*
     * 单项删除
     */
    public function dokeyword ()
    {
        $dbw = Bridge::pdo(Bridge::DB_ADMIN_W);

        $id = intval($_GET['id']);
        if (!$id)
        {
            HDshowMsg('删除失败',['type'=>'error','desc' => '参数非法',]);
        }
        else
        {
            $dbw->query("delete from Filter where Id = $id ");
            $this->adminLog('删除关键字', $id, 'del', $id);
            $url= "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
            HDshowMsg('删除成功',['type'=>'success','desc' => '','url'=> $url]);
        }
    }

    /*
     * 添加关键字
     */
    public function doAddKeyword ()
    {
        $keywords = trim($_POST['content']);
        $keyword = explode("\n", $keywords);
        $data = [];

        foreach($keyword as $k => $v)
        {
            $v = trim($v);
            if (!empty($v))
            {
                $data[$v] = $v;
            }
        }
        if (\Safe\Filter::addFilter($data))
        {
            $this->adminLog('添加关键字', 0, 'Insert', serialize($keywords));
            self::output(['title'=>'添加成功','type'=>'success']);
        }
        self::output(['title'=>'添加失败','type'=>'error']);
    }

    /*
     * 修改
     */
    public function doModify()
    {
        $new = strval($_POST['name']);
        $id = intval($_POST['id']);

        //检查关键字是否存在
        $dbr = Bridge::pdo(Bridge::DB_ADMIN_R);
        $isThere = $dbr->prepare("SELECT Id FROM `Filter` WHERE Word = :new");
        $isThere -> execute([':new' => $new]);
        $data = $isThere->fetch();

        if ($data)
        {
            self::output(['title'=>'关键字已存在','type'=>'error']);
        }

        //修改关键字
        $dbw = Bridge::pdo(Bridge::DB_ADMIN_W);
        $re = $dbw->update('Filter', ['Word' => $new], ['Id' => $id]);
        $this->adminLog('关键字修改', $id, 'update', serialize($new));
        if ($re !== false)
        {
            self::output(['title'=>'修改成功','type'=>'success']);
        }
        self::output(['title'=>'修改失败','type'=>'error']);
    }
}
$app->run();